﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using UIShare;

namespace EFong.Host
{
    public partial class FrmDBSetting : BasicForm
    {
        public const string INITIAL_CATALOG = "EFong";
        private ConnectionString connectionString = null;

        public FrmDBSetting()
        {
            InitializeComponent();

            this.btn_OK.Enabled = false;
        }

        protected override void InitFields()
        {
            connectionString = ConnectionStringManager.LoadByFile();
            if (connectionString != null)
            {
                this.txt_Password.Text = connectionString.Password;
                this.txt_Port.Text = connectionString.Port.ToString();
                this.txt_ServerName.Text = connectionString.DataSource;
                this.txt_UserName.Text = connectionString.UserID;
            }
        }

        protected override void Events()
        {
            this.btn_OK.Click += (s, e) =>
                {
                    this.save();
                };
        }

        private void save()
        {
            UIOperManager.SaveData(
                    () =>
                    {
                        connectionString = new ConnectionString
                        {
                            UserID = this.txt_UserName.Text.Trim(),
                            Password = this.txt_Password.Text.Trim(),
                            DataSource = this.txt_ServerName.Text.Trim(),
                            InitialCatalog = INITIAL_CATALOG,
                            Port = Convert.ToInt32(txt_Port.Text.Trim())
                        };

                        if (string.IsNullOrEmpty(connectionString.DataSource))
                        {
                            MsgBox.Info("请输入服务器名称.");
                        }

                        if (string.IsNullOrEmpty(connectionString.UserID))
                        {
                            MsgBox.Info("请输入用户名.");
                        }

                        return true;
                    },
                    () =>
                    {
                        ConnectionStringManager.SaveFile(connectionString);
                        return true;
                    },
                    "设置成功.",
                    () =>
                    {
                        this.Close();
                    }
                );
        }

        internal static ConnectionString SetConnectionString()
        {
            FrmDBSetting frmDBSetting = new FrmDBSetting();
            frmDBSetting.ShowDialog();
            return frmDBSetting.connectionString;
        }

        private void btn_Cancel_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void btnTest_Click(object sender, EventArgs e)
        {
            ConnectionString connectionString = new ConnectionString
            {
                UserID = this.txt_UserName.Text.Trim(),
                Password = this.txt_Password.Text.Trim(),
                DataSource = this.txt_ServerName.Text.Trim(),
                InitialCatalog = INITIAL_CATALOG,
                Port = Convert.ToInt32(txt_Port.Text.Trim())
            };

            if (string.IsNullOrEmpty(connectionString.DataSource))
            {
                MsgBox.Info("请输入服务器名称.");
            }

            if (string.IsNullOrEmpty(connectionString.UserID))
            {
                MsgBox.Info("请输入用户名.");
            }

            try
            {
                ConnectionStringManager.Test(connectionString);
            }
            catch (Exception ex) 
            {
                MsgBox.Error(ex.Message);
                btn_OK.Enabled = false;
            }

            MsgBox.Info("测试通过.");
            btn_OK.Enabled = true;
        }
    }
}
